package com.rongke.web.apix;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rongke.model.SystemConfig;
import org.apache.commons.io.IOUtils;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;

import javax.net.ssl.*;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.*;

public class TongdunYYSAuth {

    private static String PARTNER_CODE="byxt_mohe";
    private static String PARTNER_KEY="3c419161e3cd497782cf8a3327112639";

    /**
     * 同盾创建任务方法
     * @param name
     * @param idcard
     * @param mobile
     * @return
     */
    public static JSONObject createTask(String name,String idcard, String mobile){
        String url = "https://api.shujumohe.com/octopus/task.unify.create/v3?partner_code="+PARTNER_CODE+"&partner_key="+PARTNER_KEY;
        Map<String,String> bodyMap = new HashMap<>();
        bodyMap.put("channel_type","YYS");
        bodyMap.put("channel_code","100000");
        bodyMap.put("real_name",name);
        bodyMap.put("identity_code",idcard);
        bodyMap.put("user_mobile",mobile);
        String body = "channel_type=YYS&channel_code=100000&real_name="+name+"&identity_code="+idcard+"&user_mobile="+mobile;
        String result =  HttpClientUtil.sendPostSSLRequest(url,body,"UTF-8","application/x-www-form-urlencoded");
        System.out.print(result);
        return JSON.parseObject(result);
    }

    /**
     *
     * @param taskId
     * @param userName
     * @param userPass
     * @param taskStage
     * @param requestType
     * @param map
     * @return
     */
    public static JSONObject acquire(String taskId,String userName, String userPass,String taskStage,String requestType,Map<String,String> map){
        String url = "https://api.shujumohe.com/octopus/yys.unify.acquire/v3?partner_code="+PARTNER_CODE+"&partner_key="+PARTNER_KEY;
        String body = "task_id="+taskId+"&user_name="+userName+"&user_pass="+userPass+"&task_stage="+taskStage+"&request_type="+requestType;
        if(map != null){
            for (String key : map.keySet()) {
                String value = map.get(key);
                body += "&"+key+"="+value;
            }
        }

        String result = HttpClientUtil.sendPostSSLRequest(url,body,"UTF-8","application/x-www-form-urlencoded");
        System.out.print(result);
        return JSON.parseObject(result);
    }

    public static JSONObject getAll(String taskId){
        String url = "https://api.shujumohe.com/octopus/task.unify.query/v3?partner_code="+PARTNER_CODE+"&partner_key="+PARTNER_KEY;
        String body = "task_id="+taskId;
        String result = HttpClientUtil.sendPostSSLRequest(url,body,"UTF-8","application/x-www-form-urlencoded");
        return JSON.parseObject(result);
    }
}
